package net.guoyixian.admin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Order.Direction;

import net.guoyixian.admin.pojo.SysRole;
import net.guoyixian.admin.service.SysRoleService;
import net.guoyixian.common.web.DataGrid;
import net.guoyixian.common.web.JsonUtils;
import net.guoyixian.common.web.PageUtils;

@Controller
@RequestMapping("role")
public class SysRoleController extends BaseController{
	
	@Autowired
	private SysRoleService SysRoleService;

	@RequiresPermissions("role:query")
	@RequestMapping("list")
	public String list(){
		return "role/rolelist";
	}
	
	@RequiresPermissions("role:query")
	@RequestMapping("query")
	@ResponseBody
	public DataGrid query(Integer page, Integer rows, String sort, String order){
		page = (page == null) ? 1 : page;
		rows = rows > 0 ? rows : PageUtils.LIMIT;
		Direction direction = Direction.valueOf(order.toUpperCase(Locale.US));
		Order _order = new Order(PageUtils.LowerCaseWithUnder(sort), direction, null);
		List<Order> orders = new ArrayList<Order>();
		orders.add(_order);
		PageBounds bounds = new PageBounds(page, rows, orders);
		PageList<SysRole> list = SysRoleService.queryList(bounds);
		DataGrid dataGrid = new DataGrid();
		dataGrid.setRows(list);
		dataGrid.setTotal(list.getPaginator().getTotalCount());
		return dataGrid;
	}
	
	@RequiresPermissions("role:add")
	@RequestMapping("add")
	public String add(){
		return "role/roleadd";
	}
	
	@RequestMapping("queryAll")
	@ResponseBody
	public String queryAll(){
		List<SysRole> list = SysRoleService.queryAll();
		return JsonUtils.ObjectToStr(list);
	}
}
